package com.zxq.frame.uart.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.zxq.frame.core.ExchangeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
@Slf4j
public class RabbitPublishHelper {

    static RabbitPublishHelper rabbitPublishHelper = null;

    public static RabbitPublishHelper getInstance() {
        return rabbitPublishHelper;
    }

    @PostConstruct
    public void init(){
        rabbitPublishHelper = this;
    }


    @Autowired
    RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.template.network-publish-key}")
    String networkRouteKey;

    @Value("${spring.rabbitmq.template.web-publish-key}")
    String webRouteKey;



    /**
     * 发送交互数据,Web层，再次打包
     * @param exchangeDTO 交换数据
     */
    public void publishPacketToRabbit(ExchangeDTO exchangeDTO){
        log.info("publish packet json:" + JSON.toJSONString(exchangeDTO));

        rabbitTemplate.convertAndSend(webRouteKey,exchangeDTO);
    }



    /**
     * 发送交互数据，Network层，解包的
     * @param exchangeDTO 交换数据
     */
    public void publishFrameToRabbit(ExchangeDTO exchangeDTO){
        log.info("publish frame json:" + JSON.toJSONString(exchangeDTO));

        rabbitTemplate.convertAndSend(networkRouteKey,exchangeDTO);
    }






}
